export function compareTimestamp(timestamp: number) {
  const currentTime = new Date().getTime()
  const timeDiff = currentTime - timestamp

  if (timeDiff < 60000) {
    return '1分钟内'
  } else if (timeDiff < 3600000) {
    return Math.floor(timeDiff / 60000) + '分钟'
  } else if (timeDiff < 86400000) {
    return Math.floor(timeDiff / 3600000) + '小时'
  } else if (timeDiff < 2592000000) {
    return Math.floor(timeDiff / 86400000) + '天'
  } else if (timeDiff < 7776000000) {
    return Math.floor(timeDiff / 2592000000) + '月'
  } else {
    return null
  }
}

export function gotoHome() {
  uni.showModal({
    title: '提示',
    content: '页面有误将返回首页',
    showCancel: false,
    success: (res) => {
      if (res.confirm) {
        uni.navigateTo({
          url: '/pages/index/index',
        })
      }
    },
  })
}

export function gotoLogin() {
  uni.showModal({
    title: '提示',
    content: '该功能需要登录，将返回登录页面',
    showCancel: false,
    success: (res) => {
      if (res.confirm) {
        uni.navigateTo({
          url: '/pages/login/login',
        })
      }
    },
  })
}

export function gotoRegister() {
  uni.showModal({
    title: '提示',
    content: '是否前往注册页面？',
    success: (res) => {
      if (res.confirm) {
        uni.navigateTo({
          url: '/pages/register/register',
        })
      }
    },
  })
}

export function goPreview(id: number) {
  uni.navigateTo({
    url: `/pages/preview/preview?id=${id}`,
  })
}

export function goPreviewAlbum(id: number) {
  uni.navigateTo({
    url: `/pages/preview/album?id=${id}`,
  })
}

/**
 * 显示消息提示框
 * @param content 提示的标题
 */
export function toast(content: string) {
  uni.showToast({
    icon: 'none',
    title: content,
  })
}

/**
 * 显示模态弹窗
 * @param content 提示的标题
 */
export function showConfirm(content: string) {
  return new Promise((resolve) => {
    uni.showModal({
      title: '提示',
      content: content,
      cancelText: '取消',
      confirmText: '确定',
      success: function (res) {
        resolve(res)
      },
    })
  })
}

/**
 * 参数处理
 * @param params 参数
 */
export function tansParams(params: any) {
  let result = ''
  for (const propName of Object.keys(params)) {
    const value = params[propName]
    const part = encodeURIComponent(propName) + '='
    if (value !== null && value !== '' && typeof value !== 'undefined') {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          if (value[key] !== null && value[key] !== '' && typeof value[key] !== 'undefined') {
            const params = propName + '[' + key + ']'
            const subPart = encodeURIComponent(params) + '='
            result += subPart + encodeURIComponent(value[key]) + '&'
          }
        }
      } else {
        result += part + encodeURIComponent(value) + '&'
      }
    }
  }
  return result
}
